<?php

class Model_EventApplication extends Model{

	function __construct(){
		$this->Lookup = getModel('Model_Lookup');
		$this->Member = getModel('Model_Member');
	}

	/*列出全部申請的會員資料
	* $type int 1審核中和不通過驗證 2通過驗證 0或者不填 全部
	* $eventId int 產品ID
	*/
	function getAllUser($eventId=0,$type=0){
		if(!intval($eventId)) return false;
		$where="TB1.status=1 and TB1.oid=".intval($eventId);

		switch ($type) {
			case 2:
				$where .= " and (TB1.application_status=11 or TB1.application_status=7 or TB1.application_status=9) ";
				break;
			case 1:
				$where .= " and (TB1.application_status=1 or TB1.application_status=3 or TB1.application_status=5)";
				break;
			case 0:
				$where .= " ";
				break;
			default:
				return  false;
				break;
		}

		$param = array();
		$param['field'] = "TB1.mid as id,TB1.url,TB2.photo,TB2.username";
		$param['where'] = $where;
		$param['order'] = ""; //"id asc"
		$param['group'] = "";
		$param['limit'] = "";
		$param['pagesize'] =  "999";
		$param['page'] = "1";
		$param['join'] = "LEFT JOIN member_imore";
		$param['on'] = "TB1.mid = TB2.id";
		$data = db_query_hash('event_application',$param);

		return $data;
	}

	/* 獲得列表資料
	application_status
	1：申請開始，3：審核中, 5：沒有通過，7：通過；9：會員已確認申請，11會員拒絕

	* $type int 1審核中和不通過審核 2通過驗證 0或者不填 全部  3已接受邀請產品
	* $eventId int 產品ID
	* $orderby		string	 	字段名 desc 或 字段名 asc
	* $page		int			當前頁數
	* $size		int			獲取個數
	* return		array		資料數組
	*/
	function getApplicationList($eventId=0,$type=0,$orderby="TB1.id desc",$page = 1, $size = 3,$aid=0){


		$where="TB1.status=1" ;
		if(intval($eventId)) $where.=" and TB1.oid=".intval($eventId);
		if(intval($aid)) $where.=" and TB1.id=".intval($aid);


		switch ($type) {
			case 2:
				$where .= " and (application_status=11 or application_status=7 or application_status=9) ";
				break;
			case 1:
				$where .= " and (application_status=1 or application_status=3 or application_status=5)";
				break;
			case 0:
				$where .= " ";
				break;
			case 3:
				$where .= " and application_status=9";
                                break;
                        case 4:
				$where .= " and body is not null and body <>''";
				break;
			default:
				return  false;
				break;
		}

		$param = array();
		$param['field'] = "TB1.*,TB2.photo,TB2.username,TB2.email";
		$param['where'] = $where;
		$param['order'] = $orderby; //"id asc"
		$param['group'] = "";
		$param['limit'] = "";
		$param['pagesize'] = $size;
		$param['page'] = $page;
		$param['join'] = "LEFT JOIN member_imore";
		$param['on'] = "TB1.mid = TB2.id";
		$data = db_query_hash('event_application',$param);

		$age = $this->Lookup->getLookup('agegroup');

		$agearr = array();
		if($age['result']){
			foreach ($age['result'] as $value) {
				$agearr[$value['id']]=$value['name'];
			}
		}
		$body_skin = $this->Lookup->getLookup('face_skin');

		$body_skinarr = array();
		if($body_skin['result']){
			foreach ($body_skin['result'] as $value) {
				$body_skinarr[$value['id']]=$value['cname'];
			}
		}

		$result = array();
		if($data['result']){
			foreach ($data['result'] as $key => $value) {
				$conResult1 = $this->Member->getMember($value['mid']);
                                $conResult2 = $this->Member->getMemberCondition($value['mid']);
				if($conResult1['result']){
					$value['age'] = $agearr[getAgeGroup(getAge($conResult1['result'][0]['birthday']))];
				}
				if($conResult2['result']){
					$value['body_skin'] = $body_skinarr[$conResult2['result'][0]['face_skin']];
				}
				$result[$key]=$value;
			}
		}

		$data['result']=$result;

		return $data;

	}


	/* 獲得列表資料
	application_status
	1：申請開始，3：審核中, 5：沒有通過，7：通過；9：會員已確認申請，11會員拒絕

	* $type int 1審核中和不通過審核 2通過驗證 0或者不填 全部
	* $mid int 會員ID
	* $orderby		string	 	字段名 desc 或 字段名 asc
	* $page		int			當前頁數
	* $size		int			獲取個數
	* return		array		資料數組
	*/
	function getMyApplicationList($mid=0,$type=0,$orderby="TB1.id desc",$page = 1, $size = 3){
		if(!intval($mid)) return false;
		$where="TB1.status=1 and TB1.mid=".intval($mid);

		switch ($type) {
			case 2:
				$where .= " and (application_status=11 or application_status=7 or application_status=9) ";
				break;
			case 1:
				$where .= " and (application_status=1 or application_status=3 or application_status=5)";
				break;
			case 0:
				$where .= " ";
				break;
			case 3:
				$where .= " and application_status=9";
				break;
			default:
				return  false;
				break;
		}

		$param = array();
		$param['field'] = "TB1.id as application_id,TB1.oid ,TB1.mid ,TB1.application_status ,TB2.id,TB2.cover_image1 as cover,TB2.member_type,TB2.title,TB2.starttime,TB2.endtime,TB2.event_type,TB2.event_date,TB2.event_time,TB2.place,TB2.intro,TB2.content_json,TB2.brand_str,TB2.click,TB2.likes,TB2.quota,TB2.apply_num,TB2.has_detail,TB1.accept_endtime";
		$param['where'] = $where;
		$param['order'] = $orderby; //"id asc"
		$param['group'] = "";
		$param['limit'] = "";
		$param['pagesize'] = $size;
		$param['page'] = $page;
		$param['join'] = "LEFT JOIN event";
		$param['on'] = "TB1.oid = TB2.id";
		$data = db_query_hash('event_application',$param);

		return $data;

	}

}
?>